package tk.woppo.mgame;

import net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.banner.AdViewListener;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.FrameLayout;

import com.phkg.b.BManager;
import com.phkg.b.MyBMDevListner;

public class MainActivity extends Activity
{
	protected final static String TAG = "MainActivity";
	MainView view;
	public static final String WIDTH = "width";
	public static final String HEIGHT = "height";
	public static final String SCORE = "score";
	public static final String HIGH_SCORE = "high score temp";
	public static final String UNDO_SCORE = "undo score";
	public static final String CAN_UNDO = "can undo";
	public static final String UNDO_GRID = "undo";
	public static final String GAME_STATE = "game state";
	public static final String UNDO_GAME_STATE = "undo game state";
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		view = new MainView(getBaseContext());
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		view.hasSaveState = settings.getBoolean("save_state", false);
		if (savedInstanceState != null)
		{
			if (savedInstanceState.getBoolean("hasState"))
			{
				load();
			}
		}
		setContentView(view);
		// 加载酷果广告
		loadKGAds();
		// 加载有米广告
		// loadYMAds();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_MENU)
		{
			// Do nothing
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
		{
			view.game.move(2);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
		{
			view.game.move(0);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
		{
			view.game.move(3);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
		{
			view.game.move(1);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		savedInstanceState.putBoolean("hasState", true);
		save();
	}
	
	protected void onPause()
	{
		super.onPause();
		save();
	}
	
	private void save()
	{
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = settings.edit();
		Tile[][] field = view.game.grid.field;
		Tile[][] undoField = view.game.grid.undoField;
		editor.putInt(WIDTH, field.length);
		editor.putInt(HEIGHT, field.length);
		for (int xx = 0; xx < field.length; xx++)
		{
			for (int yy = 0; yy < field[0].length; yy++)
			{
				if (field[xx][yy] != null)
				{
					editor.putInt(xx + " " + yy, field[xx][yy].getValue());
				} else
				{
					editor.putInt(xx + " " + yy, 0);
				}
				if (undoField[xx][yy] != null)
				{
					editor.putInt(UNDO_GRID + xx + " " + yy, undoField[xx][yy].getValue());
				} else
				{
					editor.putInt(UNDO_GRID + xx + " " + yy, 0);
				}
			}
		}
		editor.putLong(SCORE, view.game.score);
		editor.putLong(HIGH_SCORE, view.game.highScore);
		editor.putLong(UNDO_SCORE, view.game.lastScore);
		editor.putBoolean(CAN_UNDO, view.game.canUndo);
		editor.putInt(GAME_STATE, view.game.gameState);
		editor.putInt(UNDO_GAME_STATE, view.game.lastGameState);
		editor.commit();
	}
	
	protected void onResume()
	{
		super.onResume();
		load();
	}
	
	private void load()
	{
		// Stopping all animations
		view.game.aGrid.cancelAnimations();
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		for (int xx = 0; xx < view.game.grid.field.length; xx++)
		{
			for (int yy = 0; yy < view.game.grid.field[0].length; yy++)
			{
				int value = settings.getInt(xx + " " + yy, -1);
				if (value > 0)
				{
					view.game.grid.field[xx][yy] = new Tile(xx, yy, value);
				} else if (value == 0)
				{
					view.game.grid.field[xx][yy] = null;
				}
				int undoValue = settings.getInt(UNDO_GRID + xx + " " + yy, -1);
				if (undoValue > 0)
				{
					view.game.grid.undoField[xx][yy] = new Tile(xx, yy, undoValue);
				} else if (value == 0)
				{
					view.game.grid.undoField[xx][yy] = null;
				}
			}
		}
		view.game.score = settings.getLong(SCORE, view.game.score);
		view.game.highScore = settings.getLong(HIGH_SCORE, view.game.highScore);
		view.game.lastScore = settings.getLong(UNDO_SCORE, view.game.lastScore);
		view.game.canUndo = settings.getBoolean(CAN_UNDO, view.game.canUndo);
		view.game.gameState = settings.getInt(GAME_STATE, view.game.gameState);
		view.game.lastGameState = settings.getInt(UNDO_GAME_STATE, view.game.lastGameState);
	}
	
	/**
	 * 有米广告
	 */
	private void initYMAds()
	{
		AdManager.getInstance(this).init("您的应用发布ID", "您的应用密钥", false);
	}
	
	private void loadYMAds()
	{
		// 实例化 LayoutParams（重要）
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);
		// 设置广告条的悬浮位置
		layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角
		// 实例化广告条
		AdView adView = new AdView(this, AdSize.FIT_SCREEN);
		adView.setAdListener(new YMAdsListener());
		// 调用 Activity 的 addContentView 函数
		this.addContentView(adView, layoutParams);
	}
	
	private class YMAdsListener implements AdViewListener
	{
		@Override
		public void onReceivedAd(AdView adView)
		{
			// 切换广告并展示
		}
		
		@Override
		public void onSwitchedAd(AdView adView)
		{
			// 请求广告成功
		}
		
		@Override
		public void onFailedToReceivedAd(AdView adView)
		{
			// 请求广告失败
		}
	}
	
	/**
	 * 加载酷果广告
	 */
	private void loadKGAds()
	{
		BManager.showTopBanner(MainActivity.this, BManager.CENTER_BOTTOM, BManager.MODE_APPIN, Const.COOID, Const.QQ_CHID);
		BManager.setBMListner(new ADSListener());
	}
	
	private class ADSListener implements MyBMDevListner
	{
		@Override
		public void onInstall(int i)
		{
			Log.i(TAG, "安装成功");
		}
		
		@Override
		public void onShowBanner()
		{
			Log.i(TAG, "广告显示成功");
		}
	}
}
